﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Chaos.model
{
    class appinfo : INotifyPropertyChanged, stream.streamable
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify(string property)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public player findbyname(String in_name)
        {
            for (int i = 0; i < Players.Count; i++)
            {
                if (Players[i].Name == in_name)
                {
                    return Players[i];
                }
            }
            return null;
        }

        public player createplayer(String in_name)
        {
            player p = new player();
            p.Name = in_name;
            Players.Add(p);
            Notify("Players");

            if (IsHost)
            {
                network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
                if (s != null)
                {
                    messages.process_model_appinfo msg = new messages.process_model_appinfo();
                    msg.AppInfo = this;
                    s.BroadcastMessage(msg);
                }
            }

            return p;
        }

        private ObservableCollection<player> m_players = new ObservableCollection<player>();
        public ObservableCollection<player> Players
        {
            get { return m_players; }
            set
            {
                if (m_players != value)
                {
                    m_players = value;
                    Notify("Players");
                }
            }
        }

        public bool IsHost
        {
            get
            {
                return HostName == Properties.Settings.Default.UserName;
            }
        }

        private String m_hostname;
        public String HostName
        {
            get { return m_hostname; }
            set
            {
                if (m_hostname != value)
                {
                    m_hostname = value;
                    Notify("HostName");
                    Notify("IsHost");
                }
            }
        }

        public void start()
        {
            Players = new ObservableCollection<player>();
            HostName = Properties.Settings.Default.UserName;
            createplayer(HostName);
        }


        public virtual void serialize(stream.stream io_Stream)
        {
            io_Stream.begin("appinfo")
                .serialize("hostname", ref m_hostname)
                .serialize("players", ref m_players)
            .end();

            if (io_Stream.isinputstream())
            {
                Notify("Players");
                Notify("HostName");
                Notify("IsHost");           
            }
        }
    }
}
